import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';

class BusinessClerkManageEdit extends StatefulWidget {
  static final String sName = 'BusinessClerkManageEdit';

  @override
  _BusinessClerkManageEditState createState() => _BusinessClerkManageEditState();
}

class _BusinessClerkManageEditState extends State<BusinessClerkManageEdit> {
  Map<String, dynamic> _updateDataSource = {};
  BaseRequestFormProvider _baseRequestFormProvider;

  @override
  Widget build(BuildContext context) {
    String userId = this._getUserId(context);
    return BaseScaffoldComponent(
      title: '完善资料',
      child: BaseScreenRefreshComponent(
        needRefreshIndicator: true,
        request: BaseScreenRequestModel('/x3-service-vip-bills-center/vipbills/vipbillclerks/clerk/query/$userId', method: BaseHttpMethodEnum.GET),
        builder: _buildUserInfoWidget,
      ),
    );
  }

  Widget _buildUserInfoWidget(BaseHttpResultModel result) {
    dynamic resultData = result.data;
    Map<String, dynamic> dataSource = BaseUtils.getKeyData(resultData, 'dataSource');
    String userSex = BaseUtils.getKeyData(dataSource, 'userSex').toString();
    String userSexName = BaseUtils.getKeyData(dataSource, 'userSexName').toString();
    String userGroupId = BaseUtils.getKeyData(dataSource, 'userGroupId').toString();
    String userGroupName = BaseUtils.getKeyData(dataSource, 'userGroupName').toString();
    Map<String, dynamic> params = {
      ...dataSource,
      'userSex': {'key': userSex, 'value': userSexName},
      'userGroupId': {'key': userGroupId, 'value': userGroupName}
    };
    _updateDataSource = params;
    return BaseScreenRefreshComponent(
      request: BaseScreenRequestModel('/x3-service-bills-center/bills/users/app/get/user/info', method: BaseHttpMethodEnum.GET),
      builder: _buildUserInfoForm,
    );
  }

  Widget _buildUserInfoForm(BaseHttpResultModel result) {
    if (_baseRequestFormProvider == null) {
      dynamic resultData = result.data;
      List<dynamic> userRequests = BaseUtils.getKeyData(resultData, 'request');
      List<BaseHttpRequestModel> requests = BaseFormTransformUtil.transformToListRequestData(userRequests);
      _baseRequestFormProvider = BaseRequestFormProvider(requests: requests, dataSource: _updateDataSource);
    }
    return SingleChildScrollView(child: BaseCardComponent(child: BaseRequestForm(baseRequestFormProvider: _baseRequestFormProvider, requestSaveData: _saveFormData)), padding: EdgeInsets.only(bottom: 20.0));
  }

  void _saveFormData(BaseHttpRequestModel request, Map<String, dynamic> data, {bool notShowLoading}) async {
    if (notShowLoading == null || !notShowLoading) BaseComponentUtils.showLoadingDialog(context);
    String service = '/bills/users/app/save/user/info';
    String userId = this._getUserId(context);
    Map<String, dynamic> _data = _baseRequestFormProvider.getDataMapKey(request);
    Map<String, dynamic> params = {'userId': userId, ..._data};
    BaseHttpResultModel result = await dioUtil.fetch(service, params: params);
    if (notShowLoading == null || !notShowLoading) BaseComponentUtils.hideLoadingDialog(context);
    if (result.success == true) {
      this._updateDataSource = this._updateDataSource;
    }
  }

  String _getUserId(BuildContext context) {
    dynamic arguments = BaseRouterUtil.getArguments(context);
    return BaseUtils.getKeyData(arguments, 'userId').toString();
  }
}
